/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "ClusterSendIM.h"
#include "QQClient.h"
ClusterSendIMPacket::ClusterSendIMPacket (QQClient& cl,
	const string msg, 
	uint32_t _received,
	uint16_t _messageId,
	uint8_t  _fragmentSequence
	) :
    OutPacket(cl, 
	    QQ_CMD_CLUSTER_CMD,
	    SEND_IM_COUNT),
    message(msg),
    clusterId(_received),
    messageId(_messageId),
    totalFragments(0x00),
    fragmentSequence(_fragmentSequence)
{}

int ClusterSendIMPacket::putBody(uint8_t* buf) const {

    uint8_t* cur = buf;

    cur += writeBufB(cur, QQ_CLUSTER_CMD_SEND_IM_EX);  // 命令类型

    cur += writeBufDw(cur, clusterId);		    // 群内部ID
    // 后面数据的长度，这个长度需要根据消息长度和字体名称长度计算才能知道，
    // 所以先来产生消息和字体名称字节数组，先占个位置
    // 
    uint8_t* lenpos = cur;
    cur += 2;
    // 未知的2字节
    cur += writeBufW(cur, 0x0001);
    // 分片数
    cur += writeBufB(cur, totalFragments);
    // 分片序号
    cur += writeBufB(cur, fragmentSequence);
    // 消息id
    cur += writeBufW(cur, messageId);
    // 未知4字节
    cur += writeBufDw(cur, 0);

    // 以0结束的消息
    cur += writeBufData(cur, reinterpret_cast<const uint8_t*>(message.c_str()),
	    message.length());
    // 字体信息
    cur += font.putBody(cur);

    // 写入长度
    uint16_t len = cur - lenpos - 2;
    writeBufW(lenpos, len);

    return cur - buf;
}

